package com.yeraphael.controller;


import com.yeraphael.entity.Emp;
import com.yeraphael.entity.EmpQueryParam;
import com.yeraphael.entity.PageBeandf;
import com.yeraphael.entity.Result;
import com.yeraphael.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
//    public Result getAllEmps(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
    public Result getAllEmps(EmpQueryParam param) {
        log.info("分页查询：{}, {},{}, {},{}, {}", param.getName(), param.getGender(), param.getBegin(), param.getEnd(), param.getPage(), param.getPageSize());
        PageBeandf pageBean = empService.page(param);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("{}", emp.toString());
        empService.save(emp);
        return Result.success();
    }

//    public Result delete(Integer[] ids)
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("{}", ids);
        empService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getEmp(@PathVariable Integer id) {
        log.info("{}", id);
        Emp emp = empService.getEmp(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息：{}", emp.toString());
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listCl() {
        List<Emp> empList = empService.getListCl();
        return Result.success(empList);
    }

}
